package com.briup.product_source.service.Impl;

import com.briup.product_source.bean.Basic.BaseAccount;
import com.briup.product_source.dao.Basic.BaseAccountMapper;
import com.briup.product_source.dao.Ext.BaseAccountExtMapper;
import com.briup.product_source.service.AccountService;
import com.briup.product_source.util.Assert;
import com.briup.product_source.util.JwtUtil;
import com.briup.product_source.util.ResultCode;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static com.briup.product_source.constant.BriupConstant.*;
import static com.briup.product_source.util.ResultCode.*;


@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private BaseAccountExtMapper baseAccountExtMapper;
    @Override
    public String login(String username, String password) {
        BaseAccount account = baseAccountExtMapper.selectLoginInfo(username);
        Assert.isNull(account, ResultCode.USER_IS_NULL);
        account.getPassword();
        Assert.isTrue(password.equals(account.getPassword()),ResultCode.PASSWORD_ERROR);
        Assert.isTrue(account.getStatus() == ACCOUNT_ENABLE,USER_STATUS_ERROR);
        return JwtUtil.sign(account.getAccountId(),
                 ImmutableMap.of("username",username,"realname",account.getRealname()));
    }

    @Override
    public BaseAccount findLoginUser(String token) {
        return baseAccountExtMapper.selectByPrimaryKey(
                JwtUtil.getUserId(token));
    }
}
